package com.example.sprotsapi.Controller;

import com.example.sprotsapi.Pojo.user;
import com.example.sprotsapi.Service.userService;
import com.example.sprotsapi.Utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class userController {

    @Autowired
    userService userService;

    @RequestMapping(value = "/getall",method = RequestMethod.GET)
    public Result getAll() {
        List<user> userList = userService.getAll();
        return Result.success(userList);
    }

    @RequestMapping(value = "/pages",method = RequestMethod.GET)
    public Result pages(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        List<user> userList = userService.pages((pageNum - 1) * pageSize,pageSize);
        List<user> total = userService.getAll();
        return Result.success(userList,pageNum,pageSize,total.size());
    }

    @RequestMapping("/getuses")
    public Result getusers(@RequestBody user user) {
        List<user> userList = userService.pages((user.getPageNum() - 1) * user.getPageSize(),user.getPageSize());
        return Result.success(userList);
    }

    @RequestMapping("/set")
    public Result setUser(@RequestBody user user){
        userService.setUser(user);
        return Result.success();
    }

    @RequestMapping("/getUser")
    public Result getUser(@RequestParam Integer uid){
        return Result.success(userService.getUser(uid));
    }

    @RequestMapping("/getUserName")
    public Result getUser(@RequestBody user user){
        return Result.success(userService.getUserName(user.getUsername()));
    }

    @RequestMapping("/del")
    public Result delUser(@RequestBody user user){
        userService.delUser(user.getUid());
        return Result.success();
    }

    @RequestMapping("/update")
    public Result updateUser(@RequestBody user user){
        userService.updateUser(user);
        return Result.success();
    }
}
